<?php
namespace app\mobile\controller;
//use Seller\Controller\HomeController;
use think\Db;

class Draw extends MobileBase
{
	public function index(){
		$id=I('get.id');
		if(IS_AJAX){
			$data['prize_name']=I('post.prize_name');
			$data['prize_level']=I('post.prize_level');
			$data['draw_id']=$id;
			$data['user_id']=$this->user_id;
			if($data['prize_level']>0){
				$data['winner_no']='P'.time().rand(10000,99999);
				$data['status']='wait_check';
			}
			$data['create_time']=time();

			M('draw_log')->add($data);
			echo 'success';
			exit;
		}
		$act_count=M('draw_log')->where(['user_id'=>$this->user_id,'draw_id'=>$id])->count();
		$is_vip=M('users')->where(['user_id'=>$this->user_id,'mobile'=>['neq','']])->count();
		$draw=M('draw')->where('id',$id)->find();
		$remain_act_count=$draw['int_count']-$act_count;

		$p=[0,0.006,0.31,0.062,0.31,0.31];//设置奖项概率
		$int=[0,20,1000,200,1000,1000];
		$prize=M('draw_log')->field('count(*) as zj_int,prize_level')->group('prize_level')->select();
		foreach ($prize as $key => $value) {
			if($value['zj_int']-$int[$value['prize_level']]>=0){
				$p[$value['prize_level']]=0;
			}
		}


		$this->assign('draw',$draw);
		$this->assign('id',$id);
		$this->assign('p',$p);
		$this->assign('is_vip',$is_vip);
		$this->assign('remain_act_count',$remain_act_count);
		return $this->fetch();
	}
	public function draw_list(){
		$id=I('get.id');
		$list=M('draw_log')->where(['user_id'=>$this->user_id,'draw_id'=>$id])->select();
		$this->assign('list',$list);
		return $this->fetch();
	}
	public function addcard(){
		$cardid=I('post.cardid');
		$timestamp=I('post.timestamp');
		$nonceStr=I('post.nonceStr');
		if(IS_AJAX){
			$jssdk = new \app\mobile\logic\Jssdk($this->weixin_config['appid'], $this->weixin_config['appsecret']);
			$cardSign= $jssdk->getCardSign($this->user['openid'],$timestamp,$nonceStr,$cardid);
			//var_dump($cardSign);
			echo $cardSign['sign'];
			/*$html=<<<EOF
	<script type="text/javascript">
	function showcard(){
	wx.addCard({
	  cardList: [
	    {
	      cardId: $cardid,
	      cardExt: '{"code":"","openid":"","nonce_str":$nonceStr,"timestamp": $timestamp, "signature":$cardSign[sign]}'
	    }
	  ],
	  success: function (res) {
	    alert('已添加卡券：' + JSON.stringify(res.cardList));
	  },
	  cancel: function (res) {
	    alert(JSON.stringify(res))
	  }
	});
}
showcard();
	</script>
EOF;*/

		}
	}
}